using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using WebCrawlerTools.data;
using WebCrawlerTools.page;

namespace WebCrawlerTools.web
{
    public class WebEngine : INet
    {
        public event EventHandler WebLoaded;
        private static System.Windows.Forms.WebBrowser webwin;

        public static System.Windows.Forms.WebBrowser Web
        {
            get
            {
                return webwin;
            }
            set
            {
                webwin = value;
            }
        }
        public Uri Uri
        {
            get
            {
                return webwin.Url;
            }
            set
            {
                webwin.Url =value;
            }
        }

        #region INet 

        public void OnLoad(object sender, WebBrowserProgressChangedEventArgs e)
        {
            if (null != WebLoaded)
            {
                if (e.CurrentProgress == 0 && e.MaximumProgress == 0)
                {
                    WebLoaded.Invoke(sender, e);
                }
            }
        }
        public void OpenUri(string url)
        {
            Log.Inst().Write(url);
            webwin.Navigate(url);
        }
        public void ExcuteJs(Js js)
        {
            webwin.Document.InvokeScript(js.Method, js.Parameters.ToArray()); 
        }
        public String HtmlSrc()
        {
            if (null == webwin || null == webwin.Document || null == webwin.Document.Body)
            {
                return "";
            }
            return webwin.Document.Body.InnerHtml;
        }

        #endregion
    }
}
